#!/bin/bash
>ftp_down2.txt
while true
do
	n_date=`date`
	nc  -z 172.16.145.128 21 &>/dev/null
	if (( $? == 0 ))
	then		
		echo "$n_date ftp server is running"
		if [ -s down.txt ]
		then
			[ -s run.txt ]
			if (( $? != 0 ))
			then
				date +%s >run.txt
				r_time2=$(date)
		   	else
				r_time=$(cat run.txt)
				(( down_time=r_time-d_time ))
				echo -e "\tstop time:$r_time2;\n\tdown total time:${down_time}s" >>ftp_down2.txt
				>down.txt
			fi
		fi
	else
		[ -s down.txt ]
		if (( $? != 0 ))
		then
			date +%s >down.txt
			d_time2=$(date)
			((i++))
			echo -e "$i.\tftp down start time:$d_time2" >>ftp_down2.txt
			echo "$n_date Warnning!! ftp server is down" 
		else
		 	d_time=$(cat down.txt)
			echo "$n_date Warnning!! ftp server is down" 
			>run.txt
		fi
	fi
sleep 1
done 
